//
//  ChatRoomViewController.m
//  E_ChatRoom
//
//  Created by rimi1 on 15/6/26.
//  Copyright (c) 2015年 TJ. All rights reserved.
//

#import "ChatRoomViewController.h"
#import "ChatRoomPushViewController.h"
#import "CreateChatRoomView.h"

@interface ChatRoomViewController ()<XMPPRoomDelegate, UITableViewDataSource, UITableViewDelegate> {
    NSArray *_chatRoom;
    NSDictionary *_chatRoomDic;
    UIView *_backgroundView;
    CreateChatRoomView *_creatChatRoomView;
}

- (void)initializeDataSource;
- (void)initializeAppearance;

@property (nonatomic, strong) UITableView *tableView;

@end

@implementation ChatRoomViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initializeDataSource];
    [self initializeAppearance];
    
}

- (void)initializeAppearance {
    self.view.backgroundColor = [UIColor whiteColor];
    _tableView = [[UITableView alloc] initWithFrame:[FlexibleFrame frameWithIPhone5Frame:CGRectMake(0, 0, CGRectGetWidth(self.view.bounds), CGRectGetHeight(self.view.bounds) - 64 - 49)]];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    [self.view addSubview:_tableView];
    
    UIBarButtonItem *creatChatRoomItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(createChatRoom)];
    creatChatRoomItem.tintColor = [UIColor whiteColor];
    self.navigationItem.rightBarButtonItem = creatChatRoomItem;
}

- (void)initializeDataSource {
    _chatRoom = @[@"创建聊天室",@"加入聊天室",@"我的聊天室"];
    _chatRoomDic = @{@"我创建的聊天室":@"",
                     @"我加入的聊天室":@""};
}

#pragma mark - 导航栏按钮点击事件
- (void)createChatRoom {
    // 先加一个透明视图
    _backgroundView = [[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    _backgroundView.backgroundColor = [UIColor clearColor];
    [self.view.window addSubview:_backgroundView];
    
    _creatChatRoomView = [[CreateChatRoomView alloc] initWithFrame:CGRectMake(100, 100, 20, 20)];
    _creatChatRoomView.backgroundColor = [UIColor orangeColor];
    [_backgroundView addSubview:_creatChatRoomView];
    [UIView animateWithDuration:0.25 animations:^{
        _creatChatRoomView.frame = CGRectMake(200, 200, 100, 100);
    }];
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    NSLog(@"123");
    [UIView animateWithDuration:0.25 animations:^{
        _creatChatRoomView.frame = CGRectMake(100, 100, 20, 20);
    } completion:^(BOOL finished) {
        [_backgroundView removeFromSuperview];
    }];
}

#pragma mark - UITableVIewDataSource,UITableVIewDelegate methods
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return _chatRoomDic.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _chatRoom.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *identifier = @"chatRoomCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
//    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.textLabel.text = _chatRoom[indexPath.row];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 80;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [_tableView deselectRowAtIndexPath:indexPath animated:NO];
    
    ChatRoomPushViewController *ChatRoomVC = [[ChatRoomPushViewController alloc] init];
    [self.navigationController pushViewController:ChatRoomVC animated:YES];
}

@end
